/* Author: smayo@google.com

*/


ZOO.WorldBakingDay = function(data){
  var pub = {
    data: data,
    index: 0,
    player:null,
    triggerListener:null,
    recog:null,
    snapshot:null,
    readyTakeSnapshot: true,

    init: function(){
      console.log('wbd init');
      $(window).on('resize', this.resizeVideo);
      $('#cook-along-btn').on('click', $.proxy(function(){
        slide('next');
      }, this));

      $('#voice-btn').on('click', $.proxy(function(){
        slide('next');
        this.initRecog();
        this.nextStep(0);
        this.resizeVideo();
      }, this));

      $('.step').on('click', $.proxy(function(e){
        //check for breakout button class which identifies a button that doesn't do the default action but still lives inside the .step
        console.log(e.target);
        if($(e.target).hasClass('breakout-btn')){
          this.initSnapshot();
          this.player.pauseVideo();
          return;
        }
        console.log(e.currentTarget);
        this.nextStep(Number($(e.currentTarget).attr('data-index')));
      }, this));

      $('#take-snap').on('click', $.proxy(function(e){
        if(this.readyTakeSnapshot){
          this.snapshot.takeSnapshot();
          this.readyTakeSnapshot = false;
          $('#take-snap').text('Retake Snap?');
          $('#photo').addClass('photo-taken');
          $('#share-ui').show('slow');
          $('#white-fade').show().fadeOut('slow');
        }else{
          this.snapshot.retakeSnapshot();
          this.readyTakeSnapshot = true;
          $('#take-snap').text('Take Snap!');
          $('#photo').removeClass('photo-taken');
          $('#share-ui').hide('slow');
          $('#white-fade').hide();
        }
      }, this));

      $('#share').on('click', $.proxy(function(){
        $('#photo').clone()
          .addClass('photo-clone send-snap').removeClass('photo-taken')
          .attr('id', '')
          .css({'top':$('#photo').offset().top+10, 'left':$('#photo').offset().left+10})
          .appendTo('#wrap')
          .on('webkitAnimationEnd', function(e){
            $(this).addClass('hide');
            $(this).remove();
          });
      }, this));

      $('.brand').on('click', $.proxy(function(){
        window.location.reload();
      }, this));

      return this;
    },
    initRecog: function(){
      this.recog = new ZOO.Recog().init();
      $(this.recog).on('recogResult', $.proxy(function(result, data){
        console.log(data);
        this.processSpeechResult(data);
      }, this));
    },
    initSnapshot: function(){
      this.snapshot = new ZOO.Snapshot().init();
      slide('next');
    },
    setPlayer: function(_player){
      console.log('setPlayer');
      this.player = _player;
      $(this.player).on('ready', $.proxy(function(){
        console.log('player ready');
        //this.nextStep(0);
        this.resizeVideo();
      }, this));
    },
    nextStep: function(i){
      console.log('nextStep: ', i);

      clearInterval(this.triggerListener);

      this.index = i;
      this.player.seekToAndPlay(this.data[this.index].start);
      $('.step').removeClass('on').addClass('off').eq(this.index).toggleClass('on off');

      //add listener for choice trigger
      this.triggerListener = setInterval( $.proxy(this.checkTrigger, this), 500);
    },
    checkTrigger: function(){
      var elapsed = this.player.getCurrentTime();
      //console.log(elapsed);
      if (elapsed > this.data[this.index].end){
        this.endOfStep();
        clearInterval(this.triggerListener);
      }

      //console.log(elapsed);

    },
    endOfStep: function(){
      console.log('endOfStep');
      this.player.pauseVideo();
      //update UI

    },
    resizeVideo: function(){
      //console.log('rsv');
      var height = $('#player').width() / (16/9);
      $('#player').attr({width: '100%', height: height});
    },
    processSpeechResult: function(data){
      console.log(data.transcript);
      switch(trim(data.transcript)){
        case "stop":
          console.log('stop');
          this.player.pauseVideo();
          this.speechAlert("computer heard: Stop");
          break;
        case "play":
          console.log('play');
          this.player.playVideo();
          this.speechAlert("computer heard: Play");
          break;
        case "next":
        case "skip":
          console.log('next/skip');
          this.nextStep(this.index+1);
          this.speechAlert("computer heard: Next/Skip");
          break;
          break;
        case "back":
          this.nextStep(this.index-1);
          this.speechAlert("computer heard: Back");
          break;
        default:
          this.speechAlert("computer says no");
          break;

      }
    },
    speechAlert: function(msg){
      $("span", ".alert").text(msg).closest('.alert').addClass('in');
    },

  }
  return pub;
};
